package com.neu.servlet.ww;

import com.neu.service.ww.GoodsService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@WebServlet(name = "DelOneServlet",urlPatterns = "/DelOneServlet.do")
public class DelOneServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String g_id=request.getParameter("g_id");
//        System.out.println(g_id.getClass()+g_id);
        String num="-";
        HttpSession session=request.getSession();
        List<Map<String,Object>> carList= (List<Map<String, Object>>) session.getAttribute("carList");
        for (int i = 0; i < carList.size(); i++) {
//            System.out.println(carList.get(i).get("G_ID").getClass()+carList.get(i).get("G_ID").toString());
//            System.out.println(Integer.parseInt(carList.get(i).get("G_ID").toString())==Integer.parseInt(g_id));
            if (Integer.parseInt(carList.get(i).get("G_ID").toString())==Integer.parseInt(g_id)){
                num+= carList.get(i).get("num").toString();
                carList.remove(i);
            }
        }
        GoodsService goodsService=new GoodsService();
        int row=goodsService.doUpdateNumById(g_id,num);
        if (row>0){
            session.setAttribute("carList",carList);
        }
//        System.out.println(carList);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
